<template>
  <div class="login">
    <div class="right_tit">奉建高速隧道监测平台</div>
    <div class="login_left_tit" />
    <!-- <div class="login_form_wrap" /> -->
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">登录</h3>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          class="username"
          type="text"
          auto-complete="off"
          placeholder="账号"
        >
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password" style="margin-bottom:28px;">
        <el-input
          v-model="loginForm.password"
          class="password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item v-if="captchaOnOff" prop="code">
        <el-input
          v-model="loginForm.code"
          class="code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" class="login-code-img" @click="getCode">
        </div>
      </el-form-item>
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <!-- :loading="loading" -->
        <el-button
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div v-if="register" style="float: right;">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>技术支持：首讯科技股份有限公司</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from '@/api/login'
import Cookies from 'js-cookie'
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: 'Login',
  data() {
    return {
      codeUrl: '',
      cookiePassword: '',
      loginForm: {
        username: '',
        password: '',
        rememberMe: false,
        code: '',
        uuid: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '请输入您的账号' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入您的密码' }
        ],
        code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff
        if (this.captchaOnOff) {
          this.codeUrl = 'data:image/gif;base64,' + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    getCookie() {
      const username = Cookies.get('username')
      const password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set('username', this.loginForm.username, { expires: 30 })
            Cookies.set('password', encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove('username')
            Cookies.remove('password')
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch('Login', this.loginForm).then(() => {
            this.$router.push({ path: '/sys_jd/statistics' }).catch(() => {})
          }).catch(() => {
            this.loading = false
            if (this.captchaOnOff) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  position: relative;
  // display: flex;
  // justify-content: center;
  // align-items: center;
  height: 100%;
  background-image: url("../assets/images/bg01.png");
  background-size: cover;
  .title {
  font-size: 28px;
  margin: 0px auto 30px auto;
  color: #ccd7e6;
}

}
.login_left_tit {
  position: absolute;
  left: 7.7vw;
  top: 16vh;
  // top: 50%;
  // transform: translateY(-50%);
  // margin-top: -50px;
  width: 600px;
  height: 545px;
  // background-image: url("../assets/images/login/tit.png");
  background-size: 100%;
  background-repeat: no-repeat;
}
.right_tit {
  position: absolute;
  top: 23px;
  right: 34px;
  font-size: 21px;
  font-weight: bold;
  background-image:-webkit-linear-gradient(left,rgba(255,255,255,0.6),#fff,rgba(255,255,255,0.6));
  -webkit-background-clip:text;
  -webkit-text-fill-color:transparent;
}

.login-form {
  position: absolute;
  right: 7.7vw;
  top: 16vh;
  // bottom: 26vh;
  background-image: url("../assets/images/login/form_bg.png");
  background-size: 100% 100%;
  width: 452px;
  height: 546px;
  padding: 64px 55px 5px 55px;
  .el-form-item {
    // margin-bottom: 32px;
  }
  .username {
    margin-top: 10px;
  }
  .username,.password {
    height: 63px;
    font-size: 22px;
    line-height: 36px;
    color: #fff;
    input {
      height: 63px;
      padding-bottom: 27px;
      padding-left: 40px;
      background-color: transparent;
      border-radius: 0;
      border: none;
      // border-color: ;
      border-bottom-color: #406196 !important;
      border-bottom-width: 1px ;
      border-bottom-style: solid;
      color: #fff;
      //  &::before {
      //   position: absolute;
      //   bottom: 0;
      //   display: block;
      //   content: ' ';
      //   width: 80%;
      //   height: 2px;
      //   background-color: #fff;
      // }
      &:focus {
        border-bottom-width: 1px;
        border-bottom-color:#fff !important;
      }
    }
  }
  .code {
    height: 50px;
    font-size: 22px;
    line-height: 48px;
    color: #fff;
    input {
      height: 50px;
      padding-left: 40px;
      border-radius: 0;
      color: #fff;
      border-width: 1px;
      border-color: #4e70a6 !important;
      background-color: transparent;
    }
    .input-icon {
      height: 48px;
      color: #fff;
    }
  }
  .input-icon {
    height: 36px;
    color: #fff;
    // height: 63px;
    // width: 14px;
    // margin-left: 2px;
  }
  .el-button  {
    font-size: 23px;
    height: 50px;
    background: #b0c1eb;
    border-radius: 0;
    border: none;
    &.is-active,
    &:active,
    &:focus,
    &:hover {
       background: #a2bbf8;
    }
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 50px;
  float: right;
  border: 1px solid #4e70a6;
  img {
    height: 100%;
    width: 100%;
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {

}
</style>
